//
//  MyCell.m
//  完整的定制一个控件
//
//  Created by clz on 2019/10/6.
//  Copyright © 2019 clz. All rights reserved.
//

#import "MyCell.h"
#import <Masonry.h>

@interface MyCell()



@end

@implementation MyCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.myCellVM = [[MyCellVM alloc]init];
        [self.myCellVM addObserver:self forKeyPath:@"titleStr" options:NSKeyValueObservingOptionNew context:nil];
        [self.myCellVM addObserver:self forKeyPath:@"subTitleStr" options:NSKeyValueObservingOptionNew context:nil];
        [self.myCellVM addObserver:self forKeyPath:@"imageName" options:NSKeyValueObservingOptionNew context:nil];

        
        
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.itemImageView = [[UIImageView alloc]init];
        self.itemImageView.clipsToBounds = YES;
        self.itemImageView.layer.cornerRadius = 4;
        self.itemImageView.contentMode = UIViewContentModeScaleAspectFill;
        [self.contentView addSubview:self.itemImageView];
        [self.itemImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(20);
            make.top.equalTo(self.contentView).offset(20);
            make.bottom.equalTo(self.contentView).offset(-20);
            make.size.mas_equalTo(CGSizeMake(60, 60));
        }];
        
        self.actionButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.actionButton setTitle:@"操作" forState:UIControlStateNormal];
        [self.actionButton setBackgroundColor:[UIColor redColor]];
        [self.actionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.actionButton addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.actionButton];
        [self.actionButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).offset(-20);
            make.centerY.equalTo(self.contentView);
            make.size.mas_equalTo(CGSizeMake(60, 30));
        }];
        
        
        self.titleItemLabel = [[UILabel alloc]init];
        self.titleItemLabel.text = @"Title";
        self.titleItemLabel.textColor = [UIColor grayColor];
        self.titleItemLabel.font = [UIFont systemFontOfSize:15];
        self.titleItemLabel.textAlignment = NSTextAlignmentLeft;
        self.titleItemLabel.numberOfLines = 0;
        [self.contentView addSubview:self.titleItemLabel];
        [self.titleItemLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.itemImageView);
            make.left.equalTo(self.itemImageView.mas_right).offset(8);
            make.right.equalTo(self.actionButton.mas_left).offset(-8);
        }];
        
        self.subTitleItemLabel = [[UILabel alloc]init];
        self.subTitleItemLabel.text = @"SubTitle";
        self.subTitleItemLabel.textColor = [UIColor lightGrayColor];
        self.subTitleItemLabel.font = [UIFont systemFontOfSize:13];
        self.subTitleItemLabel.textAlignment = NSTextAlignmentLeft;
        self.subTitleItemLabel.numberOfLines = 0;
        [self.contentView addSubview:self.subTitleItemLabel];
        [self.subTitleItemLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.itemImageView);
            make.left.equalTo(self.titleItemLabel);
            make.right.equalTo(self.titleItemLabel);
        }];
        
        
    }
    return self;
}

- (void)action:(UIButton *)sender{
    NSLog(@"触发");
    if (self.deleteBlock) {
        self.deleteBlock();
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    
    id value = change[NSKeyValueChangeNewKey];
    
    if ([keyPath isEqualToString:@"titleStr"]) {
        NSString *titleStr = value;
        self.titleItemLabel.text = titleStr;
    }else if ([keyPath isEqualToString:@"subTitleStr"]){
        NSString *subTitleStr = value;
        self.subTitleItemLabel.text = subTitleStr;
    }else if ([keyPath isEqualToString:@"imageName"]){
        NSString *imageName = value;
        self.itemImageView.image = [UIImage imageNamed:imageName];
    }
}

- (void)dealloc{
    [self.myCellVM removeObserver:self forKeyPath:@"titleStr"];
    [self.myCellVM removeObserver:self forKeyPath:@"subTitleStr"];
    [self.myCellVM removeObserver:self forKeyPath:@"imageName"];

}



@end
